<?php
class CiulevelsController extends AppController {

	var $name = 'Ciulevels';
	
	var $components = array(
			'Auth'=>array(
				'userModel' => 'Ciuuser',
				'loginError' => "Usuario/Clave equivocada",
				'loginAction' => array(
	                'controller' => 'ciuusers',
	                'action' => 'login',
	                'plugin' => false,
	               'admin' => false,
	                )
	            )
                , 'Session');
				

	function index() {
		$this->Ciulevel->recursive = 0;
		$this->set('ciulevels', $this->paginate());
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid ciulevel', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('ciulevel', $this->Ciulevel->read(null, $id));
	}

	function add() {
		if (!empty($this->data)) {
			$this->Ciulevel->create();
			if ($this->Ciulevel->save($this->data)) {
				$this->Session->setFlash(__('The ciulevel has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The ciulevel could not be saved. Please, try again.', true));
			}
		}
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid ciulevel', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Ciulevel->save($this->data)) {
				$this->Session->setFlash(__('The ciulevel has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The ciulevel could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Ciulevel->read(null, $id);
		}
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for ciulevel', true));
			$this->redirect(array('action'=>'index'));
		}
		$rels = $this->Ciulevel->Ciucourse->find('count', array('conditions' => array('Ciucourse.ciulevel_id' => $id)));
		if( $rels < 1){
			if ($this->Ciulevel->delete($id)) {
				$this->Session->setFlash(__('Ciulevel deleted', true));
				$this->redirect(array('action'=>'index'));
			}else{
				$this->Session->setFlash(__('Ciulevels was not deleted', true));
			}
		}else{
			$this->Session->setFlash(__('Ciulevels was not empty', true));
		}
		$this->redirect(array('action' => 'index'));
	}
}
